/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.wx.web;

import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.common.refun.CollectionUtil;
import com.jeesite.modules.common.refun.StringUtil;
import com.jeesite.modules.wx.entity.WxActivity;
import com.jeesite.modules.wx.entity.WxBusiness;
import com.jeesite.modules.wx.entity.WxBusinessGroup;
import com.jeesite.modules.wx.entity.WxBusinessGroupOrgConfig;
import com.jeesite.modules.wx.service.WxActivityService;
import com.jeesite.modules.wx.service.WxBusinessGroupService;
import com.jeesite.modules.wx.service.WxBusinessService;
import com.jeesite.modules.wx.service.WxUserActivityAfterService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

/**
 * 商家核销统计
 * @author wangjianxin
 * @version 2018-09-28
 */
@Controller
@RequestMapping(value = "${adminPath}/wx/wxBusinessAfter")
public class WxBusinessAfterController extends BaseController {

	@Autowired
	private WxBusinessService wxBusinessService;
	@Autowired
	private WxUserActivityAfterService wxUserActivityAfterService;
	@Autowired
	private WxActivityService wxActivityService;
	@Autowired
	private WxBusinessGroupService wxBusinessGroupService;

	/**
	 * 商家核销list
	 */
	@RequiresPermissions("wx:wxBusiness:view")
	@RequestMapping(value = {"list", ""})
	public String list(WxBusiness wxBusiness, Model model) {
		model.addAttribute("wxBusiness", wxBusiness);
		return "modules/wx/wxBusinessAfterList";
	}

	/**
	 * 商家核销listData
	 *
	 * type=1代表活动结束了
	 * type=0代表活动进行中
	 */
	@RequiresPermissions("wx:wxBusiness:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<WxBusiness> listData(WxBusiness wxBusiness, HttpServletRequest request, HttpServletResponse response,
									 @RequestParam(value = "activityType",required = false) String activityType,
									 @RequestParam(value = "activityId",required = false) String activityId){
		if(StringUtil.isEmpty(activityType)){
			activityType = "0";
		}
		if(StringUtil.isEmpty(activityId)){
			return null;
		}
		//查询活动绑定的商家分组
		WxActivity wxActivity = wxActivityService.get(activityId);
		Long groupId = wxActivity.getBusinessGroupId();
		//根据groupId查询商家
		WxBusinessGroup wxBusinessGroup = new WxBusinessGroup();
		wxBusinessGroup.setConfigGroupId(groupId);
		List<WxBusinessGroup> wxBusinessGroupList = wxBusinessGroupService.findList(wxBusinessGroup);
		//商家list
		List<WxBusiness> result = new LinkedList<>();
		if(CollectionUtil.isNotEmpty(wxBusinessGroupList)){
			WxBusinessGroup findBusinessGroup = wxBusinessGroupList.get(0);
			String businessIds = findBusinessGroup.getBusinessIds();
			if(StringUtil.isNotEmpty(businessIds)){

				String[] businessIdsArray = businessIds.split(",");
				int len = businessIdsArray.length;
				for(int i = 0; i < len ;i++){
					String businessId = businessIdsArray[i];
					WxBusiness temp = wxBusinessService.getBase(businessId);
					Integer count = wxUserActivityAfterService.afterCount(businessId,activityType,activityId);
					temp.setAfterCount(count);
					//添加,注意旧数据的排重
					if(!result.contains(temp)){
						result.add(temp);
					}
				}
			}

		}


//		List<WxBusiness> allBusiness = wxBusinessService.queryFastAll();
//		for(WxBusiness t : allBusiness){
//			Integer count = wxUserActivityAfterService.afterCount(t.getId(),activityType);
//			t.setAfterCount(count);
//		}

		//排序倒叙
		Collections.sort(result, new Comparator<WxBusiness>() {
			@Override
			public int compare(WxBusiness o1, WxBusiness o2) {
				return o2.getAfterCount() - o1.getAfterCount();
			}
		});


		wxBusiness.setGroupId(groupId);
		Page<WxBusiness> page = wxBusinessService.findPage(new Page<WxBusiness>(request, response), wxBusiness);
		int pageNo = page.getPageNo();
		int pageSize = page.getPageSize();

		pageNo -=1;
		pageNo *= pageSize;
		pageSize += pageNo;
		if(pageSize >= result.size()){
			pageSize = result.size();
		}
		List<WxBusiness> newList = result.subList(pageNo,pageSize);
		page.setList(newList);

		//排序
		return page;
	}



}